package com.tuyue.police.controller.ys;

import com.tuyue.police.api.AccessTokenApi;
import com.tuyue.police.biz.IYsUserBiz;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.pojo.User;
import com.tuyue.police.util.Result;
import com.tuyue.police.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/6/1.
 * @Modified By:
 */
@RestController
@RequestMapping("ys/user")
public class YsLoginController {

    @Autowired
    private IYsUserBiz ysUserBiz;

    @Autowired
    private AccessTokenApi accessTokenApi;
    @PostMapping("regist")
    public Result regist(@RequestBody User user){
        if (ObjectUtils.isEmpty(user)) {
            throw new MyException(ResultEnum.USER_NOTNULL);
        }
        if (StringUtils.isEmpty(user.getUserName())) {
            throw new MyException(ResultEnum.USER_USERNAMENOTNULL);
        }
        if (StringUtils.isEmpty(user.getUserPassword())) {
            throw new MyException(ResultEnum.USER_PASSWORDNOTNULL);
        }
        if (StringUtils.isEmpty(user.getType())) {
            throw new MyException(ResultEnum.USER_TYPENOTNULL);
        }
        if (user.getType()<=0) {
            throw new MyException(ResultEnum.USER_TYPE_ERROR);
        }
        User newuser =ysUserBiz.regist(user);
        return ResultUtil.success(newuser);
    }

    @GetMapping("getUserAccessToken/{accountId}")
    public String getUserAccessToken(@PathVariable String accountId){
        return accessTokenApi.getUserAccessToken(accountId);
    }

    @PostMapping("login")
    public Result login(@RequestParam String userName,@RequestParam String userPassword){
        User user = ysUserBiz.login(userName, userPassword);
        return ResultUtil.success(user);
    }
}
